// pages/apply/apply.js
let app = getApp()
let request = require('../../utils/request')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    pageAllHeight: app.globalData.pageAllHeight,
    scrollHeight: app.globalData.pageAllHeight,
    applyNumberDialog: false,
    pageNum: 1,
    pageSize: 20,
    pageFlag: false,
    subscribeList: [],
    setStatusDialog: false,
    reserveList: [],
    reserveData: {},
    parkId: 0,
    reserveRule: {},
    reserveParkSpace: 0,
    rejectDialog: false,
    passDialog: false,
    rejectCar: '',
    passCar: '',
    rejectId: 0,
    passId: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.firstOpen == 'true') {
      this.showSetStatusDialog()
    }
    this.setData({
      parkId: wx.getStorageSync('parkInfo').parkId
    })
    this.getReserveParking()
    this.getSubscribeList()
  },
  onReady: function () {
    let that = this
    let height
    wx.createSelectorQuery().select('.apply-detial').boundingClientRect(rect => {
      console.log(rect)
      height = rect.height;
      that.setData({
        scrollHeight: app.globalData.pageAllHeight - height * 2 - 20
      })
    }).exec();
  },
  /**
   * 获取预约规则
   */
  getReserveParking() {
    request.http(
      '/promotion/reserve-rule/get-by-park-id', 
      'GET', 
      { parkId: this.data.parkId }
    ).then(res => {
      let data = res.data
      if (data.code == 200) {
        this.setData({
          reserveRule: data.data,
          reserveParkSpace: data.data == null ? 0 : data.data.reserveParkSpace
        })
      }
    }).catch(err => {
      
    })
  },
  /**
   * 减今日可约车位数
   */
  subApplyToday() {
    let reserveParkSpace = this.data.reserveParkSpace
    if (reserveParkSpace <= 0) {
      wx.showToast({ title: '最少不低于0个', icon: 'none' })
      return
    }
    this.setData({
      reserveParkSpace: --reserveParkSpace
    })
  },
  /**
   * 设置今日可约车位数
   */
  setApplyToday(e) {
    this.setData({ reserveParkSpace: e.detail.value })
  },
  /**
   * 增加今日可约车位数
   */
  addApplyToday() {
    let reserveParkSpace = this.data.reserveParkSpace
    this.setData({ reserveParkSpace: ++reserveParkSpace })
  },
  /**
   * 减明日可约车位数
   */
  subApplyTommor() {
    let reserveParkSpace = this.data.reserveParkSpace
    if (reserveParkSpace <= 0) {
      wx.showToast({ title: '最少不低于0个', icon: 'none'  })
      return
    }
    this.setData({ reserveParkSpace: --reserveParkSpace })
  },
  /**
   * 修改可预约车位数
   */
  setReserveParking() {
    request.http(
      '/promotion/reserve-rule/update-reserve-park-space', 
      'POST', 
      { id: this.data.reserveRule.id, reserveParkSpace: this.data.reserveParkSpace }
    ).then(res => {
        let data = res.data
        if (data.code == 200) {
          wx.showToast({ title: '修改预约车位数成功' })
          this.setData({
            applyNumberDialog: false,
            'reserveRule.reserveParkSpace': this.data.reserveParkSpace
          })
        }
      }).catch(err => {
      
      })
  },
  /**
   * 打开编辑弹窗
   */
  showApplyNumberDialog() {
    this.setData({
      applyNumberDialog: true
    })
  },
  /**
   * 关闭编辑弹窗
   */
  closeApplyNumberDialog() {
    this.setData({
      applyNumberDialog: false
    })
  },
  /**
   * 获取预约列表
   */
  getSubscribeList(next) {
    let that = this
    let parmas = {
      pageNum: this.data.pageNum,
      pageSize: this.data.pageSize,
      status: 2,
      parkId: wx.getStorageSync('parkInfo').parkId
    }
    request.http('/order-reserve/park-reserve-record/get-reserve-record', 'GET', parmas)
      .then(res => {
        let data = res.data
        let subscribeList = that.data.subscribeList
        let pageFlag = that.data.pageFlag
        if (data.code == 200) {
          if (next) {
            if (data.data.records.length > 0) {
              subscribeList = subscribeList.concat(data.data.records)
              pageFlag = false
            } else {
              pageFlag = true
            }
          } else {
            subscribeList = data.data.records
          }
          that.setData({
            subscribeList,
            pageFlag
          })
        }
      }).catch(err => {
      
      })
  },
  /**
   * 触底加载
   */
  scrollTolower() {
    if (this.data.pageFlag) {
      return
    }
    this.setData({
      pageFlag: true,
      pageNum: this.data.pageNum + 1
    })
    this.getSubscribeList(true)
  },
  /**
   * 驳回申请
   */
  bindReject(e) {
    let index = e.target.dataset.index
    let id = e.target.dataset.id
    this.setData({
      rejectDialog: true,
      rejectCar: this.data.subscribeList[index].carNumber,
      rejectId: id
    })
  },
  /**
   * 取消
   */
  rejectCancel() {
    this.setData({
      rejectDialog: false
    })
  },
  /**
   * 驳回
   */
  rejectSuccess() {
    this.examineAndApprove(this.data.rejectId, 3)
  },
  /**
  * 通过申请
  */
  bindPass(e) {
    let index = e.target.dataset.index
    let id = e.target.dataset.id
    this.setData({
      passDialog: true,
      passCar: this.data.subscribeList[index].carNumber,
      passId: id
    })
  },
  /**
   * 取消
   */
  passCancel() {
    this.setData({
      passDialog: false
    })
  },
  /**
   * 通过
   */
  passSuccess() {
    this.examineAndApprove(this.data.passId, 2)
  },
  /**
   * 通过申请
   */
  // bindPass(e) {
  //   let that = this
  //   let id = e.target.dataset.id
  //   wx.showModal({
  //     title: '操作确认',
  //     content: '是否确认通过车主预约申请？',
  //     success(res) {
  //       if (res.confirm) {
  //         that.examineAndApprove(id, 2)
  //       }
  //     }
  //   })
  // },
  /**
   * 审批
   */
  examineAndApprove(id, status) {
    var _this = this;
    request.http(
      '/order-reserve/park-reserve-record/audit-reserve', 
      'POST',
      {id:id, status:status}
    ).then(res => {
      let data = res.data;
      if (data.code == 200) {
        wx.showToast({ 
          title: status == 3 ? '已驳回' : '预约已通过', 
          icon: 'none',
          success:function(){
            setTimeout(function () {
              _this.setData({ })
              let reserveParkSpace = _this.data.reserveRule.reserveParkSpace
              _this.setData({ 
                rejectDialog: false, 
                passDialog: false,
                pageNum: 1,
                'reserveRule.reserveParkSpace': status == 2 ? --reserveParkSpace : reserveParkSpace
              })
              _this.getSubscribeList()
            }, 2000) 
          }
        })
      }
    }).catch(err => {
      
    })
  },
  /**
   * 打开预约设置弹窗
   */
  showSetStatusDialog() {
    let that = this
    let parmas = {
      parkId: wx.getStorageSync('parkInfo').parkId
    }
    request.http('/promotion/reserve-rule/getReserveRuleByParkId', 'GET', parmas)
      .then(res => {
        let data = res.data
        let reserveData = {}
        if (data.code == 200) {
          reserveData = {
            amountUp: data.data[0].amount,
            amountDown: data.data[1].amount,
            reserveParkSpace: data.data[0].reserveParkSpace
          }
          that.setData({
            reserveList: data.data,
            reserveData,
            setStatusDialog: true
          })
        }
      }).catch(err => {
      
      })
  },
  /**
   * 减车位数
   */
  subNumber() {
    let reserveParkSpace = this.data.reserveData.reserveParkSpace
    if (reserveParkSpace <= 0) {
      wx.showToast({
        title: '最少不低于0个',
        icon: 'none'
      })
      return
    }
    reserveParkSpace--
    this.setData({
      'reserveData.reserveParkSpace': reserveParkSpace
    })
  },
  /**
   * 增加车位数
   */
  addNumber() {
    let reserveParkSpace = this.data.reserveData.reserveParkSpace
    reserveParkSpace++
    this.setData({
      'reserveData.reserveParkSpace': reserveParkSpace
    })
  },
  /**
   * 设置车位数
   */
  setNumber(e) {
    let val = e.detail.value
    this.setData({
      'reserveData.reserveParkSpace': val
    })
  },
  /**
   * 减上午服务费
   */
  subAmountUp() {
    let amountUp = this.data.reserveData.amountUp
    if (amountUp <= 0) {
      wx.showToast({
        title: '最少不低于0个',
        icon: 'none'
      })
      return
    }
    amountUp--
    this.setData({
      'reserveData.amountUp': amountUp
    })
  },
  /**
   * 增加上午服务费
   */
  addAmountUp() {
    let amountUp = this.data.reserveData.amountUp
    amountUp++
    this.setData({
      'reserveData.amountUp': amountUp
    })
  },
  /**
   * 设置上午服务费
   */
  setAmountUp(e) {
    let val = e.detail.value
    this.setData({
      'reserveData.amountUp': val
    })
  },
  /**
   * 减上午服务费
   */
  subAmountDown() {
    let amountDown = this.data.reserveData.amountDown
    if (amountDown <= 0) {
      wx.showToast({
        title: '最少不低于0个',
        icon: 'none'
      })
      return
    }
    amountDown--
    this.setData({
      'reserveData.amountDown': amountDown
    })
  },
  /**
   * 增加上午服务费
   */
  addAmountDown() {
    let amountDown = this.data.reserveData.amountDown
    amountDown++
    this.setData({
      'reserveData.amountDown': amountDown
    })
  },
  /**
   * 设置上午服务费
   */
  setAmountDown(e) {
    let val = e.detail.value
    this.setData({
      'reserveData.amountDown': val
    })
  },
  /**
   * 关闭预约设置弹窗
   */
  closeSetStatusDialog() {
    this.setData({
      setStatusDialog: false
    })
  },
  /**
   * 设置预约功能
   */
  setReserve() {
    let that = this
    let parmas = {
      amountDown: this.data.reserveData.amountDown,
      amountUp: this.data.reserveData.amountUp,
      reserveParkSpace: this.data.reserveData.reserveParkSpace,
      reserveFeeScaleIdDown: this.data.reserveList[1].id,
      reserveFeeScaleIdUp: this.data.reserveList[0].id
    }
    request.http('/promotion/reserve-rule/updateReserveFeeScale', 'POST', parmas, 0)
      .then(res => {
        let data = res.data
        if (data.code == 200) {
          wx.showToast({
            title: '设置成功',
          })
          that.setData({
            setStatusDialog: false
          })
        }
      }).catch(err => {
      
      })
  },
})